#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool bp1[201][201], bp2[201][201];
    vector<vector<int>> ret;
    int dx[4] = { 1, -1, 0, 0 };
    int dy[4] = { 0, 0, 1, -1 };
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        for (int i = 0; i < heights.size(); i++)
            if (bp1[i][0] == false) dfs(heights, i, 0, bp1);
        for (int j = 0; j < heights[0].size(); j++)
            if (bp1[0][j] == false) dfs(heights, 0, j, bp1);
        for (int i = 0; i < heights.size(); i++)
            if (bp2[i][heights[0].size() - 1] == false) dfs(heights, i, heights[0].size() - 1, bp2);
        for (int j = 0; j < heights[0].size(); j++)
            if (bp2[heights.size() - 1][j] == false) dfs(heights, heights.size() - 1, j, bp2);
        for (int i = 0; i < heights.size(); i++)
        {
            for (int j = 0; j < heights[0].size(); j++)
                if (bp1[i][j] && bp2[i][j])
                {
                    vector<int> tmp;
                    tmp.push_back(i), tmp.push_back(j);
                    ret.push_back(tmp);
                }
        }
        return ret;
    }
    void dfs(vector<vector<int>>& heights, int x, int y, bool(*bp)[201])
    {
        bp[x][y] = true;

        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < heights.size() && b >= 0 && b < heights[a].size() && heights[a][b] >= heights[x][y] && bp[a][b] == false) dfs(heights, a, b, bp);
        }
    }
};